package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.exception.OverideLayout;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterFragment;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.TopListRequestFilter;
import com.mmlc.network.responsebean.TopResponseBean;
import com.mmlc.utils.ImageTools;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.XUtils;

import org.xutils.image.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

import static com.mmlc.meimei.lichengkongjian.R.id.tv_rank;


/**
 * Created by Administrator on 2016/11/24.
 */
public class MqTopFragment extends InterFragment {

    TextView tvRank;
    ImageView ivHead;
    TextView tvSellername;
    TextView tvStorename;
    TextView tvSellprice;
    TextView tvOrdernum;
    TextView tv_yuan;
    LinearLayout mine;
    RecyclerView rcMq;
    OverideLayout olOverideLayout;
    RelativeLayout rlRoot;
    TextView tv_nodata;
    public MqTopAdapter adapter;
    List<TopResponseBean.DataBean.ListBean> data ;
    TopResponseBean.DataBean.SelfBean self;

    @Override
    protected int initResourceID() {
        return R.layout.fragment_mq_rank;
    }

    @Override
    protected void initView() {
        tvRank = (TextView) view.findViewById(tv_rank);
        ivHead = (ImageView) view.findViewById(R.id.iv_head);
        tvSellername = (TextView) view.findViewById(R.id.tv_sellername);
        tvStorename = (TextView) view.findViewById(R.id.tv_storename);
        tvSellprice = (TextView) view.findViewById(R.id.tv_sellprice);
        tvOrdernum = (TextView) view.findViewById(R.id.tv_ordernum);
        tvSellprice = (TextView) view.findViewById(R.id.tv_sellprice);
        tvSellprice = (TextView) view.findViewById(R.id.tv_sellprice);
        tv_yuan = (TextView) view.findViewById(R.id.tv_yuan);
        mine = (LinearLayout) view.findViewById(R.id.mine);
        rcMq = (RecyclerView) view.findViewById(R.id.rc_mq);
        olOverideLayout = (OverideLayout) view.findViewById(R.id.ol_overideLayout);
        rlRoot = (RelativeLayout) view.findViewById(R.id.rl_root);
        tv_nodata = (TextView) view.findViewById(R.id.tv_nodata);
    }





    @Override
    public void onResume() {
        super.onResume();
        initData();

    }
    public int type;
    private void initData() {
        Bundle bundle = getArguments();
        type = bundle.getInt("type");
        LinearLayoutManager manager = new LinearLayoutManager(view.getContext());
        rcMq.setLayoutManager(manager);
        data = new ArrayList<>();
        adapter = new MqTopAdapter(getActivity(), data);
        rcMq.setAdapter(adapter);
        TopListRequestFilter filter = new TopListRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.type = type;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<TopResponseBean>() {

            @Override
            public void onSuccess(TopResponseBean result) {
                if(result!=null) {
                    if(result.getData().getList().size() == 0){
                        tv_nodata.setVisibility(View.VISIBLE);
                        return;
                    }
                    String code = result.getCode() + "";
                    data.addAll(result.getData().getList());
                    self = result.getData().getSelf();
                    adapter.notifyDataSetChanged();
                    setInfo();
                    rlRoot.setVisibility(View.VISIBLE);
                }else {
                    tv_nodata.setVisibility(View.VISIBLE);
                    return;
                }

            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess.toString());
                tv_nodata.setVisibility(View.VISIBLE);
            }
        });
    }


    private void setInfo() {
        if(self!=null){
            if(type==0){
                tvRank.setText(self.getSort_order());

            }else{
                tvRank.setText(self.getSort_order_in());
            }
            tvSellername.setText(self.getUser_name());
            tvStorename.setText(self.getStore_name());
            tvOrdernum.setText(self.getNum());
            tvSellprice.setText(self.getTotal_price());
            mine.setVisibility(View.VISIBLE);
            tv_yuan.setVisibility(View.VISIBLE);
            ImageTools.displayCircle(ivHead, self.getHeader_thumb(), R.drawable.avatar_default, true);
        }else{
            mine.setVisibility(View.GONE);
        }

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    public class MqTopAdapter extends RecyclerView.Adapter<MqTopAdapter.AuthorViewHolder > {

        List<TopResponseBean.DataBean.ListBean> data;
        Context context;

        public MqTopAdapter(Context context,List<TopResponseBean.DataBean.ListBean> data) {
            this.context = context;
            this.data = data;
        }

        @Override
        public AuthorViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mqtop_item, parent, false);
            AuthorViewHolder  viewHolder = new AuthorViewHolder (view);
            return viewHolder;
        }


        @Override
        public void onBindViewHolder(AuthorViewHolder vh, int position) {

            if(data.size()!=0){
                if(type==0){

                    vh.tv_rank.setText(data.get(position).getSort_order());


                }else{
                    vh.tv_rank.setText(data.get(position).getSort_order_in());

                }
                if(position == 0 || position == 1 || position ==2){
                    vh.tv_rank.setTextColor(Color.parseColor("#fd5858"));
                    vh.tv_price.setTextColor(Color.parseColor("#fd5858"));
                    vh.tv_num.setTextColor(Color.parseColor("#fd5858"));
                    vh.tv_yuan.setTextColor(Color.parseColor("#fd5858"));
                }
//                else if (position == 1){
//                    vh.tv_rank.setTextColor(Color.parseColor("#fc7459"));
//                    vh.tv_price.setTextColor(Color.parseColor("#fc7459"));
//                    vh.tv_num.setTextColor(Color.parseColor("#fc7459"));
//
//                }else if(position == 2){
//                    vh.tv_rank.setTextColor(Color.parseColor("#f9914b"));
//                    vh.tv_price.setTextColor(Color.parseColor("#f9914b"));
//                    vh.tv_num.setTextColor(Color.parseColor("#f9914b"));
//
//                }
                else{
                    vh.tv_rank.setTextColor(Color.parseColor("#393939"));
                    vh.tv_price.setTextColor(Color.parseColor("#1aad19"));
                    vh.tv_num.setTextColor(Color.parseColor("#979797"));
                    vh.tv_yuan.setTextColor(Color.parseColor("#1aad19"));
                }
                vh.tv_name.setText(data.get(position).getUser_name());
                vh.tv_store.setText(data.get(position).getStore_name());
                vh.tv_num.setText(data.get(position).getNum());
                vh.tv_price.setText(data.get(position).getTotal_price());

                XUtils.image(vh.iv_Head, data.get(position).getHeader_thumb(), new ImageOptions.Builder()
                        .setCircular(true)
                        .setLoadingDrawableId(R.drawable.avatar_default)
                        .setFailureDrawableId(R.drawable.avatar_default)
                        .setFadeIn(true)
                        .build());
            }
        }

        @Override
        public int getItemCount() {
            if(data == null){
                return 0;
            }
            return data.size();
        }


        class AuthorViewHolder  extends RecyclerView.ViewHolder {
            TextView tv_rank,tv_name,tv_store,tv_num,tv_price,tv_yuan;
            ImageView iv_Head;
            public AuthorViewHolder (View v) {
                super(v);
                tv_rank = (TextView) v.findViewById(R.id.tv_rank);
                tv_name = (TextView) v.findViewById(R.id.tv_sellername);
                tv_store = (TextView) v.findViewById(R.id.tv_storename);
                tv_num = (TextView) v.findViewById(R.id.tv_sellcount);
                tv_price = (TextView) v.findViewById(R.id.tv_sellprice);
                iv_Head = (ImageView) v.findViewById(R.id.img_avatat);
                tv_yuan = (TextView) v.findViewById(R.id.tv_yuan);
            }
        }
    }
}
